package com.cskaoyan.processor;

import com.cskaoyan.bean.LifeCycleBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author stone
 * @date 2022/04/25 16:29
 */
public class CustomBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization");
        if (bean instanceof LifeCycleBean) {
            LifeCycleBean lifeCycleBean = (LifeCycleBean) bean;
            String password = lifeCycleBean.getPassword();
            String newPassword = new StringBuffer(password).reverse().toString();
            lifeCycleBean.setPassword(newPassword);
            return lifeCycleBean;
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization");
        return null;
    }
}
